package com.qa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qa.common.ServerResponse;
import com.qa.dao.ContactMapper;
import com.qa.pojo.Contact;
import com.qa.pojo.User;
import com.qa.service.IContactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author
 * @Date 2020/3/13 21:42
 * @Desc
 **/
@Service
public class ContactServiceImpl implements IContactService {

    @Autowired
    private ContactMapper contactMapper;

    @Override
    public ServerResponse add(Contact contact, User user) {
        contact.setEmail(user.getEmail());
        contact.setUserId(user.getId());
        contact.setUserName(user.getName());
        contact.setStatus(1);
        int insert = contactMapper.insert(contact);
        if (insert > 0) {
            return ServerResponse.createBySuccessMessage("成功！");
        }
        return ServerResponse.createByErrorMessage("失败！");
    }

    @Override
    public PageInfo<Contact> getByEmail(String userEmail, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Contact> contactList = contactMapper.getByEmail(userEmail);
        return new PageInfo<>(contactList);
    }

    @Override
    public Contact getById(Integer id) {
        return contactMapper.selectByPrimaryKey(id);
    }

    @Override
    public ServerResponse deleteById(Integer id) {
        int insert = contactMapper.deleteByPrimaryKey(id);
        if (insert > 0) {
            return ServerResponse.createBySuccessMessage("成功！");
        }
        return ServerResponse.createByErrorMessage("失败！");
    }
}
